//
//  BaseNavigationController.m
//  XiaoHongShu
//
//  Created by Jason on 2022/6/15.
//

#import "BaseNavigationController.h"

@interface BaseNavigationController ()<UIGestureRecognizerDelegate, UINavigationBarDelegate, UINavigationControllerDelegate>

@end

@implementation BaseNavigationController

//iOS中，导航栏默认是可以侧滑，但是自定义导航之后，侧滑返回失效。

+(void)initialize {
    //自定义navBar，根据当前的nav自定义 navBar
    UINavigationBar *navBar = [UINavigationBar appearanceWhenContainedInInstancesOfClasses:@[[BaseNavigationController class]]];
    //设置导航条字体颜色和大小
    NSDictionary *dict = [NSDictionary dictionary];
    if (@available(iOS 13.0, *)) {
    // iOS 13 之后，这样设置导航条的相关属性才正确。之前的做法已失效
        dict = @{NSForegroundColorAttributeName: [UIColor tertiarySystemBackgroundColor], NSFontAttributeName: [UIFont systemFontOfSize:18]};
     UINavigationBarAppearance *navBarAppearance = [[UINavigationBarAppearance alloc]init];
     [navBarAppearance configureWithOpaqueBackground];  //Opaque: 不透明
     navBarAppearance.backgroundColor = [UIColor secondarySystemGroupedBackgroundColor];
     [navBarAppearance setTitleTextAttributes: dict];
     navBar.standardAppearance = navBarAppearance;
     navBar.scrollEdgeAppearance = navBarAppearance;
    } else {
        dict = @{NSForegroundColorAttributeName: kBlackColor, NSFontAttributeName: [UIFont systemFontOfSize:18]};
        //设置导航条背景色
        navBar.barTintColor = kWhiteColor;
        [navBar setTitleTextAttributes:dict];
    }
    //设置导航条不透明
    navBar.translucent = NO;
    //设置除导航条以外的其他字体颜色
    [navBar setTintColor:kLightGrayColor];
}

//拦截push
//使用水平滑动转换。如果视图控制器已经在堆栈中，则无效。
-(void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated {
    //判断push进来的界面是否为第一个
    if (self.childViewControllers.count > 0) {
        //push后隐藏底部栏
        viewController.hidesBottomBarWhenPushed = YES;
        //自定义导航栏左侧返回按钮
        UIImage *navBackImg = [UIImage imageNamed:@"nav_back"];
        self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[navBackImg imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(navBackItemClicked)];
    }
    [super pushViewController:viewController animated:NO];
    [self setNavigationBarHidden:NO animated:YES];
}

- (void)navBackItemClicked {
    [self popViewControllerAnimated:NO];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // 重置侧滑手势的代理
    self.interactivePopGestureRecognizer.delegate = self;
}

//侧滑手势执行的代理方法
-(BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
    //如果当前已经 push 到控制器栈中的控制器数量 大于 1时，则可以使用侧滑手势
    // 如果当前展示的控制器是根控制器就不让其响应
    if (self.viewControllers.count < 2 || self.visibleViewController == [self.viewControllers objectAtIndex:0]) {
        return NO;
    }
    return YES;
    //return self.childViewControllers.count > 1;
}

//设置状态栏样式
// ios13之前有2种：UIStatusBarStyleDefault 、UIStatusBarStyleLightContent
//ios 13之后新增：UIStatusBarStyleDarkContent
- (UIStatusBarStyle)preferredStatusBarStyle {
    if (@available (iOS 13.0, *)) {
        return UIStatusBarStyleLightContent;
    } else {
        return UIStatusBarStyleDefault;
    }
}

//这句代码的作用是，不调用自己本身的preferredStatusBarStyle方法而是调用显示的控制器的preferredStatusBarStyle方法。
- (UIViewController *)childViewControllerForStatusBarStyle {
    return self.visibleViewController;
}

//是否隐藏状态栏
-(BOOL) prefersStatusBarHidden {
    return NO;
}




/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
